package com.xindaoapp.happypet.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.BigBanner;

@SuppressWarnings("rawtypes")
public class BigViewPageActivity extends UMBaseActivity {
    private Context mContext;
    private ViewPager mViewpager;
    private TextView vp_size;
    private TextView vp_total;

    private int current = -1;
    private ArrayList list = new ArrayList();
    private List<BigBanner> mList = new ArrayList<BigBanner>();

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_viewpage);
        mContext = BigViewPageActivity.this;
        current = getIntent().getIntExtra("current", -1);
        list = getIntent().getStringArrayListExtra("list");
        mList = (List<BigBanner>) list.get(0);

        initView();
        initData();
    }

    private void initView() {
        mViewpager = (ViewPager) findViewById(R.id.viewpager);
        vp_size = (TextView) findViewById(R.id.product_detail_size);
        vp_total = (TextView) findViewById(R.id.product_detail_size_total);
    }

    private void initData() {
        prepareViewPage();
    }

    public void prepareViewPage() {
        ViewPagerAdapter adapter = new ViewPagerAdapter();
        mViewpager.setAdapter(adapter);
        mViewpager.setCurrentItem(current);
        mViewpager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(final int position) {
                vp_size.setText(position + 1 + "");
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {}

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {}
        });
        Log.e("---->", current + 1 + "");
        vp_size.setText(current + 1 + "");
        vp_total.setText("/" + mList.size());
    }

    class ViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mList.size();
        }

        /**
         * 判断出去的view是否等于进来的view 如果为true直接复用
         */
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        /**
         * 销毁预加载以外的view对象, 会把需要销毁的对象的索引位置传进来就是position
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        /**
         * 创建一个view
         */
        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            ImageView iv = new ImageView(mContext);
            iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            String uri = mList.get(position).url;
            ImageLoader.getInstance().displayImage(uri, iv);
            container.addView(iv);
            iv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    BigViewPageActivity.this.finish();
                }
            });
            return iv;
        }
    }

}
